﻿
Imports System.IO
Imports std
Imports storoomc
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl

Public Class submitpage
    Inherits cacheProcessor
    'handle /submit?url=abc&tag=abc

    Public Const _pathidentity As String = "/submit"
    Public Const urlquery As String = "url"
    Public Const tagquery As String = "tag"

    Protected Overrides Function processImpl(ByVal context As httpContext) As Boolean
        Dim url As String = Nothing
        Dim tag As String = Nothing
        url = context.url.query(urlquery)
        tag = context.url.query(tagquery)
        Dim buff As MemoryStream = Nothing
        buff = context.buff()

        Dim page As xhtmlpage = Nothing
        page = New xhtmlpage(buff)

        If isemptystring(url) OrElse isemptystring(tag) Then
            Dim delayJump As delayJump = Nothing
            delayJump = New delayJump(buff)
            delayJump.delay = 10
            delayJump.destination = strmid(inputpage._pathidentity, strlen(network.pathSeperator)) + network.questionMark _
                                    + inputpage.urlquery + network.argumentNameValueSeperator + url _
                                    + network.argumentSeperator _
                                    + inputpage.tagquery + network.argumentNameValueSeperator + tag
            page.head().addchild(delayJump)
            page.title().text = "cannot submit - " + applicationSign()
            page.body().id = "id_body"

            Dim label As label = Nothing
            label = New label(buff)
            label.text = "cannot sumit the rss feed, please input both link and tags."
            label.id = "id_error"
            page.body().addchild(label)

            Dim br As br = Nothing
            br = New br(buff)
            page.body().addchild(br)

            label = New label(buff)
            label.text = "following is the information server has got."
            label.id = "id_info"
            page.body().addchild(label)

            br = New br(buff)
            page.body().addchild(br)

            label = New label(buff)
            label.text = "link " + url
            label.id = "id_link"
            page.body().addchild(label)

            br = New br(buff)
            page.body().addchild(br)

            label = New label(buff)
            label.text = "tags " + tag
            label.id = "id_tag"
            page.body().addchild(label)

            br = New br(buff)
            page.body().addchild(br)

            label = New label(buff)
            label.text = "will redirect to the input page in " + Convert.ToString(delayJump.delay) + " seconds."
            label.id = "id_redirect"
            page.body().addchild(label)
        Else
            page.title().text = "submit result - " + applicationSign()
            page.body().id = "id_body"

            Dim label As label = Nothing
            label = New label(buff)
            label.id = "id_url"
            label.text = "url " + url
            page.body().addchild(label)
            page.body().addchild(New br(buff))

            label = New label(buff)
            label.id = "id_feedslist"
            If rashare.internalNaming(url) Then
                label.text = "do not enable using an internal naming as url, include but not only " _
                            + rashare.feedsname + ", " + rashare.tagsname + ", " + rashare.crawling
                page.body().addchild(label)
            Else
                Dim urlbytes As bytes = Nothing
                urlbytes = New bytes(encoding.utf8.GetBytes(url + character.newline))

                If Not storoom.write(rashare.feedsname, urlbytes) Then
                    raiseError("cannot write feed " + url + " to feedslist in storoom,", errorHandle.errorType.warning)
                    label.text = "failed to write feed " + url + " into feedslist in storoom."
                Else
                    label.text = "success to write feed " + url + " into feedslist in storoom."
                End If
                page.body().addchild(label)
                page.body().addchild(New br(buff))

                Dim tags As vector(Of String) = Nothing
                strsepar(tag, spaceChars, tags, True)
                Dim i As Int64
                For i = 0 To tags.size() - 1
                    label = New label(buff)
                    label.id = "id_tagurl_" + Convert.ToString(i)

                    If rashare.internalNaming(tags(i)) Then
                        label.text = "do not enable using an internal naming as tag, include but not only " _
                                    + rashare.feedsname + ", " + rashare.tagsname + ", " + rashare.crawling
                    Else
                        If Not storoom.write(tags(i), urlbytes) Then
                            raiseError("cannot add url " + url + " to tag " + tags(i), errorHandle.errorType.warning)
                            label.text = "failed to add url " + url + " to tag " + tags(i)
                        Else
                            label.text = "success to add url " + url + " to tag " + tags(i)
                        End If

                        label = New label(buff)
                        label.id = "id_tag_" + Convert.ToString(i)
                        If Not storoom.write(rashare.tagsname _
                                             , New bytes(encoding.utf8.GetBytes(tags(i) + character.newline))) Then
                            raiseError("cannot add tag " + tags(i) + " to taglist.", errorHandle.errorType.warning)
                            label.text = "failed to add tag " + tags(i) + " to tag list."
                        Else
                            label.text = "success to add tag " + tags(i) + " to tag list."
                        End If
                    End If
                    page.body().addchild(label)
                    page.body().addchild(New br(buff))
                Next
            End If
        End If

        assert(page.render(Not isdebugmode()), "failed to render submitpage.")
        Return True
    End Function

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Public Overrides Function pathidentitylen() As Long
        Return strlen(pathidentity())
    End Function
End Class
